package src.CodeTopSecondPractice;

import java.util.Stack;

public class _20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0 ; i < s.length() ; i++) {
            char cur = s.charAt(i);
            if (cur == '(' || cur == '[' || cur == '{') {
                stack.push(cur);
            }else {
                if (stack.isEmpty()) {
                    return false;
                }
                if (stack.peek() == '(' && cur == ')') {
                    stack.pop();
                }else if (stack.peek() == '[' && cur == ']') {
                    stack.pop();
                }else if (stack.peek() == '{' && cur == '}') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
